package com.neusoft.yuejian12.yuejian_12.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.javabean.UserBean;
import com.neusoft.yuejian12.yuejian_12.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.neusoft.yuejian12.yuejian_12.R.id.text_reg;

public class LoginActivity extends AppCompatActivity {

    private TextView textView;
    private EditText userName;
    private EditText userPass;
    private CheckBox rememberme;
    private Button loginBtn;

//    public LoginActivity() {
//
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
          initViews();
          initEvents();
    }
    public void initViews(){
        userName = (EditText) findViewById(R.id.uname);
        userPass = (EditText) findViewById(R.id.upass);
        rememberme = (CheckBox) findViewById(R.id.ckx_rember);
        loginBtn = (Button) findViewById(R.id.loginbtn);
    }
    public void initEvents() {

        TextView textView = (TextView) findViewById(text_reg);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel userModel = new UserModel();

                //1.发送请求
                Call<UserBean> call=userModel.login
                        (userName.getText().toString(),userPass.getText().toString());
                //2.处理结果
                Callback<UserBean> callback=new Callback<UserBean>() {
                    @Override
                    public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                        UserBean user = response.body();
                        if (!"0".equals(user.getUserid())) {
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            saveUser(user.getUserid());
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();

                        } else {
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<UserBean> call, Throwable t) {

                    }
                };
                //3.call and callback 捆绑
                call.enqueue(callback);
            }
        });
    }

    public void saveUser(String u_id){
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("username",userName.getText().toString());
        int user_id = Integer.parseInt(u_id);
        editor.putInt("user_id",user_id);
        editor.putString("userpass",userPass.getText().toString());
        editor.commit();

    }


}






